/*
Copyright 2010 Peter Krajnc, SCREENWORKER

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.screenworker.compactmvc.patterns
{
	import com.screenworker.compactmvc.core.CompactMVC;
	import com.screenworker.compactmvc.interfaces.ICommand;
	import com.screenworker.compactmvc.interfaces.IModel;
	import com.screenworker.compactmvc.vo.NoteVO;
	
	/**
	 * <code>ICommand</code> implementation.
	 * pattern class to be extended by subclasses
	 * 
	 * internally uses notification transport model
	 * 
	 * @author Peter Krajnc
	 */
	public class Command implements ICommand
	{
		private var _controller:Controller;
		
		/**
		 * Constructor 
		 * @param controller
		 * 
		 */		
		public function Command(controller:Controller)
		{
			_controller = controller;
		}
		
		/**
		 * add here the execution Code 
		 * @param note
		 * 
		 */		
		public function execute(note:NoteVO):void
		{
			
		}	
		
		/**
		 * handles incoming Notifications and broadcast to Commands
		 * @param notificationType
		 * @param data
		 * 
		 */	
		protected function sendNotification(notificationType:String, data:Object=null, delay:Number=0):void
		{		
			_controller.sendNotification( new NoteVO(notificationType, data), delay );
		}
		
		/**
		 * notify one Controller by Name
		 * @param notificationType
		 * @param data
		 * 
		 */		
		protected function sendNotificationTo(controllerName:String, notificationType:String, data:Object=null, delay:Number=0):void
		{
			CompactMVC.getInstance().sendNotificationTo(controllerName, notificationType, data, delay);
		}
		
		/**
		 * notify all controllers
		 * @param notificationType
		 * @param data
		 * 
		 */		
		protected function sendJunctionNotification(notificationType:String, data:Object=null, delay:Number=0):void
		{	
			CompactMVC.getInstance().sendJunctionNotification( notificationType, data, delay);
		}
		
		
		
		/**
		 * Removes a <code>Command</code> 
		 * @param notificationType
		 * 
		 */		
		protected function removeCommand(notificationType:String):void
		{	
			_controller.removeCommand(notificationType);
		}
		
		/**
		 * register a <code>Command</code>
		 * @param notificationType
		 * @param commandClassRef
		 * 
		 */		
		protected function registerCommand(notificationType:String, commandClassRef:Class):void
		{	
			_controller.registerCommand(notificationType, commandClassRef);
		}
		
		/**
		 * register Model 
		 * @param modelName
		 * @param modelClassRef
		 * 
		 */		
		protected function registerModel(modelName:String, modelClassRef:Class):void
		{	
			_controller.registerModel(modelName, modelClassRef);
		}
		
		/**
		 * remove Model 
		 * @param modelName
		 * 
		 */		
		protected function removeModel(modelName:String):void
		{	
			_controller.removeModel(modelName);
		}
		
		/**
		 * retrieve Model 
		 * @param modelName
		 * @return 
		 * 
		 */		
		protected function retrieveModel(modelName:String):IModel
		{
			return _controller.retrieveModel(modelName);
		}
		
		protected function isModelRegistered(modelName:String):Boolean
		{
			return _controller.isModelRegistered(modelName);
		}
		
		/**
		 * remove View(UI) and Mediator 
		 * @param viewName
		 * 
		 */		
		protected function removeView(viewName:String ):void
		{
			_controller.removeView(viewName);
		}
		
		
		protected function retrieveView(viewname:String):Object
		{
			return _controller.retrieveView(viewname);
		}
		
		protected function isViewRegistered(viewname:String):Boolean
		{
			return _controller.isViewRegistered(viewname);
		}
		
		
		
		/**
		 * returns all registered views 
		 * @return 
		 * 
		 */		
		protected function retrieveAllRegisteredViews():Array
		{
			return _controller.retrieveAllRegisteredViews();
		}
		
		
		/**
		 * retrieves the mainApplication object
		 * @return 
		 * 
		 */		
		protected function get mainApplication():*
		{
			return _controller.mainApplication;
		}
		
		/**
		 * Cleanup Controller 
		 * 
		 */		
		protected function cleanupController():void
		{
			_controller.cleanup();
		}
	}
}
